package swu.edu.cn.javaee.service;

import com.sun.security.auth.UnixNumericUserPrincipal;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import swu.edu.cn.javaee.component.Image;
import swu.edu.cn.javaee.entity.User;
import swu.edu.cn.javaee.entity.UserInfo;
import swu.edu.cn.javaee.entity.UserLogin;
import swu.edu.cn.javaee.mapper.UserMapper;

import java.sql.Struct;
import java.util.List;

import static swu.edu.cn.javaee.component.Signal.*;

@Service
public class UserService {


    @Resource
    private UserMapper userMapper;

    /**
     * 保存用户信息(phone password permission)
     * params String String String
     * result String
     * */
    public String saveUser(String phone,String password,String permission){
        User user=new User();
        user.setPhone(phone);
        user.setPassword(password);
        user.setPermission(permission);
        if(userMapper.queryUser(phone)==null){
            userMapper.saveUser(user);
            System.out.println(user);
            return SUCCESS_CODE;
        }
        else {
            System.out.println("Service 帐号已存在");
            return FAILED_CODE_PHONE_EXIST;
        }
    }

    /**
     * 根据电话号码查询是否有某个用户
     * params String
     * result String
     * */
    public String queryIs(String phone){
        User user= userMapper.queryUser(phone);
        System.out.println(user);
        if(user!=null){
            return SUCCESS_CODE;
        }
        else {
            System.out.println("Service 失败！,未查询到对应用户信息");
            return FAILED_CODE_PHONE;
        }
    }

    /**
     * 根据phone和password检查是否有用户以及用户的password是否一致
     * params String String
     * result String
     * */
    public UserLogin checkUser(String phone,String password){
        User user= userMapper.queryUser(phone);
        UserLogin userLogin=new UserLogin();
        if(user!=null){
            if (user.getPassword().equals(password)){
                if(user.getPermission().equals("0")){
                    //TODO
                    userLogin.setSignal("1");
                    userLogin.setSignal("admin");
                }else {
                    userLogin.setSignal(SUCCESS_CODE);
                }
                userLogin.setUser(user);
            }
            else {
                System.out.println("Service 失败！,用户密码不正确");
                userLogin.setSignal(FAILED_CODE_PASSWORD);
            }
        }
        else {
            System.out.println("Service 失败！,未查询到对应用户信息");
            userLogin.setSignal(FAILED_CODE_PHONE_NO_EXIST);
        }
        return userLogin;
    }

    /**
     * 修改用户头像和昵称
     * params String String String
     * result String
     * */
    public String changeUser(String phone,String avatar, String nickname){
        User user=userMapper.queryUser(phone);
        if(user!=null){
            String filepath= Image.GenerateImage(avatar);
            System.out.println(filepath);
            user.setNickname(nickname);
            user.setAvatar(filepath);
            userMapper.updateUser(user);
            return SUCCESS_CODE;
        }
        else {
            System.out.println("Service changeUser失败,用户不存在 ");
            return FAILED_CODE_PHONE_NO_EXIST;
        }
    }
    /**
     * 根据电话号码查询某个用户
     * params String
     * result User/null
     * */
    public User queryUser(String phone){
        User user=userMapper.queryUser(phone);
        if(user!=null){
            return user;
        }
        else {
            return null;
        }
    }

    public UserInfo queryListLimit(String page,String limits){
        Integer start=Integer.valueOf(page);
        Integer limit=Integer.valueOf(limits);
        UserInfo userInfo=new UserInfo();
        int count=userMapper.countUser();
        List<User> list=userMapper.queryListLimit((start-1)*limit,limit);
        if(list.size()!=0){
            userInfo.setCode("0");
            userInfo.setCount(String.valueOf(count));
            userInfo.setMsg("");
            userInfo.setData(list);
            System.out.println(userInfo);
        }
        else {
            userInfo.setCode("1");
        }
        return userInfo;
    }

    /**
     * 查询所有用户信息，装入Info返回
     * return UserInfo（code,msg,count,data[List<User>]）
     * */
    public UserInfo queryList(){
        List<User> list=userMapper.queryList();
        Integer count=userMapper.countUser();
        UserInfo userInfo=new UserInfo();
        if(list.size()!=0){
            userInfo.setCode("0");
            userInfo.setCount(String.valueOf(count));
            userInfo.setMsg("");
            userInfo.setData(list);
            return userInfo;
        }
        return null;
    }


    /**
     * 修改密码
     * params String String
     * result String
     * */
    public String changePassword(String phone,String password){
        User user=userMapper.queryUser(phone);
        if(user!=null){
            if(!user.getPassword().equals(password)){
                user.setPassword(password);
                System.out.println(user);
                userMapper.updateUser(user);
                return SUCCESS_CODE;
            }
            else {
                System.out.println(FAILED_CODE_PASSWORD_REPEAT);
                return FAILED_CODE_PASSWORD_REPEAT;
            }
        }
        else {
            return FAILED_CODE_PHONE_NO_EXIST;
        }
    }
}
